"use strict";

const { Controller } = require("egg");

class AudioResource extends Controller {
    /**
     * @api {POST} /x/v1/audio/resource/uploader 01:音频资源上传回调接口
     * @apiGroup 02:音频接口
     * @apiVersion 1.0.0
     * @apiParam {string} name 音频资源名称
     * @apiParam {string} path 音频资源地址
     * @apiParam {number} duration 音频资源时长
     * @apiParam {number} size 音频资源大小
     * @apiSuccess {number} code 10201
     * @apiError {number} code 20201
     */
    async audioResourceUploader() {
        const { ctx } = this;
        const { name, object, duration, size } = ctx.request.body;
        try {
            const audioResource = await ctx.model.AudioResource.create({ name, object, duration, size });
            ctx.returnBody(10201, "", audioResource);
        } catch (error) {
            ctx.logger.error(error);
            // 调用删除文件服务
            ctx.service.oss.ossDeleteService(object);
            ctx.returnBody(20201, "");
        }
    }
    /**
     * @api {POST} /x/v1/audio/cover/uploader/:rid 02:音频封面上传回调接口
     * @apiGroup 02:音频接口
     * @apiVersion 1.0.0
     * @apiParam (params) {number} rid 音频资源id
     * @apiParam {string} object 音频封面地址
     * @apiSuccess {number} code 10202
     * @apiError {number} code 20202
     */
    async audioCoverUploader() {
        const { ctx } = this;
        const { rid } = ctx.params;
        const { object } = ctx.request.body;
        try {
            ctx.model.AudioResource.update({
                cover: object,
            }, {
                where: { rid },
            });
            ctx.returnBody(10202, "");
        } catch (error) {
            ctx.logger.error(error);
            // 调用删除文件服务
            ctx.service.oss.ossDeleteService(object);
            ctx.returnBody(20202, "");
        }
    }
    /**
     * @api {PUT} /x/v1/audio/info/updater/:rid 03:音频资源信息修改接口
     * @apiGroup 02:音频接口
     * @apiVersion 1.0.0
     * @apiParam (params) {number} rid 音频资源id
     * @apiParam {string} name 音频资源名称
     * @apiParam {string} artist 音频资源作曲家
     * @apiParam {string} mark 音频资源简介
     * @apiSuccess {number} code 10203
     * @apiError {number} code 20203
     */
    async audioInfoUpdater() {
        const { ctx } = this;
        const { rid } = ctx.params;
        const { name, artist, mark } = ctx.request.body;
        try {
            await ctx.model.AudioResource.update({
                name,
                artist,
                mark,
            }, {
                where: { rid },
            });
            ctx.returnBody(10203, "");
        } catch (error) {
            ctx.logger.error(error);
            ctx.returnBody(20203, "");
        }
    }
    /**
     * @api {PUT} /x/v1/audio/lyric/updater/:rid 04:音频资源台词修改接口
     * @apiGroup 02:音频接口
     * @apiVersion 1.0.0
     * @apiParam (params) {number} rid 音频资源id
     * @apiParam {string} lyric 音频资源台词
     * @apiSuccess {number} code 10204
     * @apiError {number} code 20204
     */
    async audioLyricUpdater() {
        const { ctx } = this;
        const { rid } = ctx.params;
        const { lyric } = ctx.request.body;
        try {
            await ctx.model.AudioResource.update({
                lyric,
            }, {
                where: { rid },
            });
            ctx.returnBody(10204, "");
        } catch (error) {
            ctx.logger.error(error);
            ctx.returnBody(20204, "");
        }
    }
    /**
     * @api {DELETE} /x/v1/audio/resource/destroyer/:rid 05:音频资源删除接口
     * @apiGroup 02:音频接口
     * @apiVersion 1.0.0
     * @apiParam (param) {number} rid 音频资源id
     * @apiSuccess {number} code 10205
     * @apiError {number} code 20205
     */
    async audioResourceDestroyer() {
        const { ctx } = this;
        const { rid } = ctx.params;
        try {
            // 先查询这个音频资源
            const audioResource = await ctx.model.AudioResource.findByPk(rid);
            // 删除它的封面
            ctx.service.oss.ossDeleteService(audioResource.cover);
            // 删除它的资源
            ctx.service.oss.ossDeleteService(audioResource.object);
            // 删除它在数据库中的记录
            audioResource.destroy({ force: true });

            ctx.returnBody(10205, "");
        } catch (error) {
            ctx.logger.error(error);
            ctx.returnBody(20205, "");
        }
    }
    /**
     * @api {GET} /x/v1/audio/resource/finder/:rid 06:音频资源查询接口(按资源id)
     * @apiGroup 02:音频接口
     * @apiVersion 1.0.0
     * @apiParam (param) {number} rid 音频资源id
     * @apiSuccess {number} code 10206
     * @apiSuccess {Object} data 音频资源
     * @apiError {number} code 20206
     */
    async audioResourceFinder() {
        const { ctx } = this;
        const { rid } = ctx.params;
        const audioResource = await ctx.model.AudioResource.findByPk(rid);
        ctx.returnBody(10206, "", audioResource);
    }
}

module.exports = AudioResource;

